package org.charts.basic;

// *************************************************************************/
// Charada Soft
// Area de Desarrollo de Sistemas de Información
// *************************************************************************/
// Proyecto : java creator charts
// Archivo : ChartBar.java
// Descripcion : Clase ChartBar
// *************************************************************************/
// Version : 1.0.5
// Fecha de Creación: 14 de Diciembre de 2014
// Author : Daniel Andres Castillo Pedroza
// *************************************************************************/

import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Image;
import org.charts.objects.DataBar;
import org.charts.interfaces.IChart;
import org.charts.interfaces.IConfiguratorChart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.TickUnitSource;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;

public class ChartBar implements IChart, IConfiguratorChart {

    // Atributos de la clase ChartBar
    
    private JFreeChart grafico;
    private DefaultCategoryDataset dataSet;
    private BarRenderer renderer;
    private String title, category, serie;
    private PlotOrientation orientacion;
    private boolean leyend, url, informacion;
    private Font fontAxes, fontData, fontTitle;
    private Color colorBack, colorChart, colorLine;

    // Constructores de la clase ChartBar
    
    public ChartBar() {
        this("Gráfico de Barras");
    }

    public ChartBar(String title) {
        initAttributes(); setTitle(title);
    }

    // Metodos de la clase ChartBar
    
    /**
     * 
     */
    private void initAttributes() {
        setNameCategory("Eje Horizontal"); setNameSerie("Eje Vertical");
        setOrientation(PlotOrientation.VERTICAL); setVisibleLeyend(true);
        setBackground(Color.ORANGE); setColorChart(Color.WHITE);
        setColorLines(Color.BLACK); setFontChart("Segoe UI Light");
        informacion = true; url = true; dataSet = new DefaultCategoryDataset();
    }

    /**
     * 
     * @param colorLine 
     */
    public void setColorLines(Color colorLine) {
        this.colorLine = colorLine;
    }

    /**
     * 
     * @param fontAxes 
     */
    public void setFontAxes(Font fontAxes) {
        this.fontAxes = fontAxes;
    }

    /**
     * 
     * @param fontData 
     */
    public void setFontData(Font fontData) {
        this.fontData = fontData;
    }

    /**
     * 
     * @param fontTitle 
     */
    public void setFontTitle(Font fontTitle) {
        this.fontTitle = fontTitle;
    }

    /**
     * 
     * @param category 
     */
    public void setNameCategory(String category) {
        this.category = category;
    }

    /**
     * 
     * @param serie 
     */
    public void setNameSerie(String serie) {
        this.serie = serie;
    }

    /**
     * 
     * @param paint
     * @param bar 
     */
    public void setColorBar(GradientPaint paint, int bar) {
        renderer.setSeriesPaint(bar, paint);
    }

    /**
     * 
     * @param orientation 
     */
    public void setOrientation(PlotOrientation orientation) {
        this.orientacion = orientation;
    }

    /**
     * 
     * @param value
     * @param serie
     * @param detail 
     */
    public void addValue(Number value, String serie, String detail) {
        addValue(new DataBar(value,serie,detail));
    }

    /**
     * 
     * @param dataBar 
     */
    public void addValue(DataBar dataBar) {
        if (dataBar != null) {
            dataSet.addValue(dataBar.getValue(),dataBar.getSerie(),dataBar.getDetail());
        }
    }

    // Metodos sobreescritos de la clase ChartBar 
    
    @Override
    public JFreeChart getChart() {
        return grafico;
    }

    @Override
    public Image getImage() {
        return grafico.getBackgroundImage();
    }

    @Override
    public final void setTitle(String title) {
        this.title = title;
    }

    @Override
    public void setBackground(Color colorBack) {
        this.colorBack = colorBack;
    }

    @Override
    public void setColorChart(Color colorChart) {
        this.colorChart = colorChart;
    }

    @Override
    public void createChart() {
        grafico = ChartFactory.createBarChart(null,category,serie,dataSet,orientacion,leyend,informacion,url);
        grafico.setTitle(new TextTitle(title,fontTitle)); grafico.setBackgroundPaint(colorBack);
        CategoryPlot plot = (CategoryPlot) grafico.getPlot(); plot.setBackgroundPaint(colorChart);
        plot.setDomainGridlinesVisible(true); plot.setRangeGridlinePaint(colorLine);
        NumberAxis rangeEjeX = (NumberAxis) plot.getRangeAxis();
        TickUnitSource unitSource = NumberAxis.createIntegerTickUnits();
        rangeEjeX.setStandardTickUnits(unitSource); renderer = (BarRenderer) plot.getRenderer();
        renderer.setDrawBarOutline(false); CategoryAxis labelX = plot.getDomainAxis();
        labelX.setLabelFont(fontAxes); labelX.setTickLabelFont(fontData);
        ValueAxis labelY = plot.getRangeAxis(); labelY.setLabelFont(fontAxes); labelY.setTickLabelFont(fontData);
    }

    @Override
    public void setFontChart(String nameFont) {
        setFontAxes(new Font(nameFont, Font.PLAIN, 18)); setFontData(new Font(nameFont, Font.PLAIN, 12));
        setFontTitle(new Font(nameFont, Font.PLAIN, 24));
    }

    @Override
    public void setVisibleLeyend(boolean leyend) {
        this.leyend = leyend;
    }
}